<template>
  <div>
    <el-card>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-card>
    <el-card class="box-card">
      <el-row :gutter="6">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            class="input-with-select"
            clearable
            @clear="getAllUsers(queryInfo)"
          >
            <el-button
              slot="append"
              @click="getAllUsers(queryInfo)"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4" :offset="12">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>
      <!-- 用户信息表格 -->
      <el-table :data="usersList" stripe>
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="username" label="名字"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column prop="mg_state" label="状态">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @change="userState(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleUser(scope.row.id)"
            ></el-button>
            <el-tooltip
              effect="dark"
              content="分配角色"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="warning"
                icon="el-icon-setting"
                size="mini"
                @click="setRole(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 10, 20, 30]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加用户对话框 -->
    <el-dialog
      title="添加用户"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClose"
    >
      <!-- 内容主体 -->
      <el-form
        ref="addFormRef"
        :model="addForm"
        :rules="addFormRules"
        label-width="80px"
      >
        <!-- prop校验规则 -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="addForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改客户对话框 -->
    <el-dialog title="修改用户" :visible.sync="editDialogVisible" width="50%">
      <!-- 内容主体 -->
      <el-form
        ref="editFormRef"
        :model="userInfo"
        :rules="editFormRules"
        label-width="70px"
        @close="editDialogClose"
      >
        <!-- prop校验规则 -->
        <el-form-item label="用户名">
          <el-input v-model="userInfo.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userInfo.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="userInfo.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
    </el-dialog>
  <!-- 分配角色对话框 -->
    <el-dialog
      title="分配角色"
      :visible.sync="setRoledialogVisible"
      width="50%"
      @close="setRoleClose"
    >
      <div>
        <p>当前的用户：{{ userInfo.username }}</p>
        <p>当前的角色：{{ userInfo.role_ }}</p>
        <p>
          分配新角色：
          <el-select v-model="selectedRoleId" placeholder="请选择">
            <el-option
              v-for="item in rolesList"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRoledialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveRoleInfo"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import {
  changeUserState,
  addUser,
  getUserInfo,
  editUserInfo,
  deleUser,
  setRole,
  saveRoleInfo
} from '@/api/user'
export default {
  data () {
    return {
      // 请求体
      queryInfo: {
        query: '',
        // 页数
        pagenum: 1,
        // 条数
        pagesize: 2
      },
      // 用户列表
      // usersList: [],
      // 用户列表条数
      // 添加客户对话框触发条件
      addDialogVisible: false,
      editDialogVisible: false,
      // 添加用户的请求体
      addForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      // 添加用户规则
      addFormRules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            min: 2,
            max: 10,
            message: '用户名的长度在3-10个字符之间',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 15,
            message: '密码的长度在6-15个字符之间',
            trigger: 'blur'
          }
        ],
        email: [
          {
            required: true,
            message: '请输入邮箱',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 15,
            message: '邮箱的长度在6-15个字符之间',
            trigger: 'blur'
          }
        ],
        mobile: [
          {
            required: true,
            message: '请输入手机号码',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 15,
            message: '手机号码的长度在6-15个字符之间',
            trigger: 'blur'
          }
        ]
      },
      // 用户表单信息
      userFromInfo: {},
      // 修改用户表单规则
      editFormRules: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号码', trigger: 'blur' }
        ]
      },
      // 分配角色对话框触发
      setRoledialogVisible: false,
      // 添加角色对话框数据
      userInfo: {
        email: '',
        mobile: ''
      },
      userId: '',
      // 角色列表
      rolesList: [],
      selectedRoleId: ''
    }
  },
  created () {
    this.getAllUsers(this.queryInfo)
  },
  methods: {
    // 获取用户列表
    getAllUsers (queryInfo) {
      this.$store.dispatch('getUserList', queryInfo)
    },
    // 监听列表条数
    handleSizeChange (NewSize) {
      this.queryInfo.pagesize = NewSize
      this.getAllUsers(this.queryInfo)
    },
    // 监听列表页数
    handleCurrentChange (NewSize) {
      this.queryInfo.pagenum = NewSize
      this.getAllUsers(this.queryInfo)
    },
    // 修改用户状态
    async userState (userinfo) {
      // console.log(userinfo);
      const res = await changeUserState(userinfo.id, userinfo.mg_state)
      if (res.meta.status !== 200) {
        userinfo.mg_state = !userinfo.mg_state
        return this.$message.error('用户状态更新失败')
      }
      this.$message.success('用户状态更新成功')
    },
    // 关闭对话框
    addDialogClose () {
      this.$refs.addFormRef.resetFields()
    },
    // 添加用户
    addUser () {
      this.$refs.addFormRef.validate(valida => {
        if (!valida) return
        addUser(this.addForm).then(res => {
          if (res.meta.status !== 201) {
            return this.$message.error('添加用户失败')
          }
          this.$message.success('添加用户成功')
          this.addDialogVisible = false
          this.getAllUsers(this.queryInfo)
        })
      })
    },
    // 获取对话框数据
    async showEditDialog (id) {
      this.userId = id
      const res = await getUserInfo(this.userId)
      if (res.meta.status !== 200) return this.$message.error('获取用户信息失败')
      this.userInfo = res.data
      this.editDialogVisible = true
      // console.log(id);
    },
    // 关闭
    editDialogClose () {
      this.$refs.editFormRef.resetFields()
    },
    // 修改用户
    editUserInfo () {
      this.$refs.editFormRef.validate(async valida => {
        if (!valida) return
        const res = await editUserInfo(this.userId, this.userInfo)
        if (res.meta.status !== 200) return this.$message.error('更新用户信息失败')
        this.editDialogVisible = false
        this.getAllUsers(this.queryInfo)
        this.$message.success('更新用户信息成功')
      })
    },
    // 删除
    deleUser (id) {
      this.$confirm('您是否要删除该用户?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleUser(id).then((res) => {
            if (res.meta.status !== 200) return this.$message.error('删除用户失败')
            this.getAllUsers(this.queryInfo)
            this.$message.success('删除用户成功')
          })
        })
        .catch(() => this.$message.info('已取消删除'))
    },
    // 获取分配角色数据
    async setRole (userInfo) {
      this.userInfo = userInfo
      this.setRoledialogVisible = true
      const res = await setRole()
      if (res.meta.status !== 200) return this.$message.error('获取角色失败')
      this.rolesList = res.data
    },
    // 保存角色信息
    async saveRoleInfo () {
      if (!this.selectedRoleId) {
        this.$message.error('请选择角色')
      }
      const res = await saveRoleInfo(this.userInfo.id, { rid: this.selectedRoleId })
      if (res.meta.status !== 200) {
        return this.$message.error('更新失败')
      }
      this.$message.success('更新成功')
      this.getAllUsers(this.queryInfo)
      this.setRoledialogVisible = false
    },
    // 关闭分配角色对话框
    setRoleClose () {
      this.selectedRoleId = ''
      this.userInfo = ''
    }
  },
  computed: {
    ...mapState({
      usersList: state => state.user.usersList,
      total: state => state.user.total
    })
  }
}
</script>
